/**
 * This file is part of RailEGo
 * Copyright (c) 2023 Guo Tingjin <dev@peercat.cn>
 * RailEGo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * RailEGo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

export namespace StringUtils {
  export const GetDayOfWeek = (month: number, day: number) => {
    const d = new Date(`${new Date().getFullYear()}-${month + 1}-${day}`);
    switch (d.getDay()) {
      case 1:
        return "星期一";
      case 2:
        return "星期二";
      case 3:
        return "星期三";
      case 4:
        return "星期四";
      case 5:
        return "星期五";
      case 6:
        return "星期六";
      case 0:
        return "星期日";
    }
  }

  export const GetDayOfWeekIndex = (month: number, day: number) => {
    if (month == new Date().getMonth()) {
      if (day == new Date().getDate())
        return "今天";
      if (day == new Date().getDate() + 1)
        return "明天";
      if (day == new Date().getDate() + 2)
        return "后天";
    }
    const d = new Date(`${new Date().getFullYear()}-${month + 1}-${day}`);
    switch (d.getDay()) {
      case 1:
        return "星期一";
      case 2:
        return "星期二";
      case 3:
        return "星期三";
      case 4:
        return "星期四";
      case 5:
        return "星期五";
      case 6:
        return "星期六";
      case 0:
        return "星期日";
    }
  }

  export const formatDate = (month: number, day: number, format?: string) => {
    if (!format) {
      return `${month.toString().padStart(2, '0')}月${day.toString().padStart(2, '0')}日`;
    }
  }

  /**
   * 将Date对象转化为对应的日期文本，format可用如下
   * Y - 年
   * M - 月，自动纠正日期，且用0填补空位
   * D - 日，自动填补空位
   * m - 分钟，自动填补空位
   * s - 秒，自动填补空位
   */
  export const formatDateByObj = (date: Date, format: string): string => {
    const formatArray = format.split('');
    const result: string[] = [];
    while (formatArray.length > 0) {
      const unit = formatArray.pop();
      switch (unit) {
        case 'Y':
          result.push(date.getFullYear().toString());
          break;
        case 'M':
          result.push((date.getMonth() + 1).toString().padStart(2, '0'));
          break;
        case 'D':
          result.push(date.getDate().toString().padStart(2, '0'));
          break;
        case 'm':
          result.push(date.getMinutes().toString().padStart(2, '0'));
          break;
        case 's':
          result.push(date.getSeconds().toString().padStart(2, '0'));
          break;
        default:
          result.push(unit);
      }
    }
    return result.reverse().join('');
  }

  export const removeSpace = (str: string): string => {
    while (str.lastIndexOf(' ') != -1) {
      str = str.replace(' ', '');
    }
    return str;
  }

  export const trimString = (str: string, trimKey?: string): string => {
    while (str.lastIndexOf(trimKey ?? ' ') != -1) {
      str = str.replace(trimKey ?? ' ', '');
    }
    return str;
  }
}